var http = require('http')
var fs = require('fs')
var server = http.createServer();
server.on('request', function (req, res) {
    if (req.url === '/index') {
        fs.readFile('./code/resource/index.html', function (err, result) {
            if (err) {
                res.setHeader('Content-Type', 'text/plain; charset=utf-8')
                res.end('文件读取失败！' + err)
            } else {
                res.setHeader('Content-Type', 'text/html; charset=utf-8')
                res.end(result)
            }
        })
    } else if (req.url === '/jpg') {
        fs.readFile('./code/resource/bg.jpg', function (err, result) {
            if (err) {
                res.setHeader('Content-Type', 'text/plain; charset=utf-8')
                res.end('文件读取失败！' + err)
            } else {
                res.setHeader('Content-Type', 'image/jpeg; charset=utf-8')
                res.end(result)
            }
        })
    } else {
        console.log("404 Not Found." + req.url)
    }
})

server.listen(3000, function () {
    console.log('Server is running...')
})